#include "snake.h"
Snake::Snake()
{
  this -> x[0] = 1;
  this -> y[0] = 3;
  this -> x[1] = 0;
  this -> y[1] = 3;

  this -> length = 2;
  this -> dir = RIGHT;
  this -> alive = true;
}

void Snake::move()
{
  for(int i=this->length-1;i>0;i--)          
    /*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/
  {
    this -> x[i] = this -> x[i-1];
    this -> y[i] = this -> y[i-1];
  }
  switch(this -> dir)
  {
  case UP:
    this -> y[0]--;
    if(this->y[0]<0)
    {
    this->y[0]=0;
    this->alive=false;
    }
    break;
  case DOWN:
    this -> y[0]++;
    if(this->y[0]>7)
    {
    this->y[0]=7;
    this->alive=false;
    }
    break;
  case LEFT:
    this -> x[0]--;
    if(this->x[0]<0)
    {
    this->x[0]=0;
    this->alive=false;
    }
    break;
  case RIGHT:
    this -> x[0]++;
    if(this->x[0]>7)
    {
    this->x[0]=7;
    this->alive=false;
    }
    break;
  }
}





